package com.serverapk.ad;

import java.util.ArrayList;
import java.util.List;

import com.serverapk.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ImgRollView extends LinearLayout{
	private Context context;
	private ViewPager mViewPager;
    private TextView mTvPagerTitle;
    private LinearLayout linearLayoutDots;
    
    private List<ImageView> mImageList;//轮播的图片集合
    private String[] mImageTitles;//标题集合
    private int previousPosition = 0;//前一个被选中的position
    private List<View> mDots;//小点

    private static boolean firstFlag = true;
    private static boolean nextFlag = false;
    
    // 在values文件假下创建了pager_image_ids.xml文件，并定义了4张轮播图对应的id，用于点击事件
    private int[] imgae_ids = new int[]{R.id.pager_image1,R.id.pager_image2,R.id.pager_image3,R.id.pager_image4};

	public ImgRollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context= context;
		init();
		initData();//初始化数据
        initView();//初始化View，设置适配器
	}
	
	public void init(){
		// 加载布局
        View view = LayoutInflater.from(context).inflate(R.layout.activity_imgview, this);
        // 获取控件
        mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
        mTvPagerTitle = (TextView) view.findViewById(R.id.tv_pager_title);
        linearLayoutDots = (LinearLayout) view.findViewById(R.id.lineLayout_dot);
	}
	
	/**
     * 第二步、初始化数据（图片、标题、点击事件）
     */
    public void initData() {
        //初始化标题列表和图片
        mImageTitles = new String[]{" "," "," "," "};
//        int[] imageRess = new int[]{R.drawable.img0,R.drawable.img1,R.drawable.img2,R.drawable.img3};
        int[] imageRess = new int[]{R.drawable.img0};

        //添加图片到图片列表里
        mImageList = new ArrayList<ImageView>();
        ImageView iv;
        for (int i = 0; i < imageRess.length; i++) {
            iv = new ImageView(context);
            iv.setBackgroundResource(imageRess[i]);//设置图片
            iv.setId(imgae_ids[i]);//顺便给图片设置id
            iv.setOnClickListener(new pagerImageOnClick());//设置图片点击事件
            mImageList.add(iv);
        }

        mDots = addDots(linearLayoutDots,fromResToDrawable(context,R.drawable.ic_dot_normal),mImageList.size());//其中fromResToDrawable()方法是我自定义的，目的是将资源文件转成Drawable


    }

    //图片点击事件
    public class pagerImageOnClick implements OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.pager_image1:
                    Toast.makeText(context, "图片1被点击", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.pager_image2:
                    Toast.makeText(context, "图片2被点击", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.pager_image3:
                    Toast.makeText(context, "图片3被点击", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.pager_image4:
                    Toast.makeText(context, "图片4被点击", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
    /**
     *  第三步、给PagerViw设置适配器，并实现自动轮播功能
     */
    public void initView(){
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(mImageList, mViewPager);
        mViewPager.setAdapter(viewPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //伪无限循环，滑到最后一张图片又从新进入第一张图片
            	int newPosition = position % mImageList.size();
                // 把当前选中的点给切换了, 还有描述信息也切换
                mTvPagerTitle.setText(mImageTitles[newPosition]);//图片下面设置显示文本
                //设置轮播点
                LayoutParams newDotParams = (LayoutParams) mDots.get(newPosition).getLayoutParams();
                newDotParams.width = 24;
                newDotParams.height = 24;
                mDots.get(newPosition).setLayoutParams(newDotParams);
                mDots.get(newPosition).setBackground(fromResToDrawable(context,R.drawable.ic_dot_focused));
                
                if(nextFlag){
                	LayoutParams oldDotParams = (LayoutParams) mDots.get(previousPosition).getLayoutParams();
                    oldDotParams.width = 16;
                    oldDotParams.height = 16;
                    mDots.get(previousPosition).setLayoutParams(oldDotParams);
                    mDots.get(previousPosition).setBackground(fromResToDrawable(context,R.drawable.ic_dot_normal));
                }

                // 把当前的索引赋值给前一个索引变量, 方便下一次再切换.
                previousPosition = newPosition;
                nextFlag = true;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        setFirstLocation();
    }

    /**
     * 第四步：设置刚打开app时显示的图片和文字
     */
    public void setFirstLocation() {
        mTvPagerTitle.setText(mImageTitles[previousPosition]);
        // 把ViewPager设置为默认选中Integer.MAX_VALUE / t2，从十几亿次开始轮播图片，达到无限循环目的;
        int m = (Integer.MAX_VALUE / 2) % mImageList.size();
        int currentPosition = Integer.MAX_VALUE / 2 - m;
        mViewPager.setCurrentItem(currentPosition);
    }

    /**
     * 第五步: 设置自动播放,每隔PAGER_TIOME秒换一张图片
     */
   
    public void palyView(){
    	if(firstFlag){
    		mViewPager.setCurrentItem(mViewPager.getCurrentItem());
    		firstFlag = false;
    	} else {
    		mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
    	}
    }
    



    /**
     * 资源图片转Drawable
     * @param context
     * @param resId
     * @return
     */
    public Drawable fromResToDrawable(Context context, int resId) {
        return context.getResources().getDrawable(resId);
    }


    /**
     * 动态添加一个点
     * @param linearLayout 添加到LinearLayout布局
     * @param backgount 设置
     * @return
     */
    public int addDot(final LinearLayout linearLayout, Drawable backgount) {
        final View dot = new View(context);
        LayoutParams dotParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        dotParams.width = 16;
        dotParams.height = 16;
        dotParams.setMargins(4,0,4,0);
        dot.setLayoutParams(dotParams);
        dot.setBackground(backgount);
        dot.setId(View.generateViewId());
        linearLayout.addView(dot);
        return dot.getId();
    }

    /**
     * 添加多个轮播小点到横向线性布局
     * @param linearLayout
     * @param backgount
     * @param number
     * @return
     */
    public List<View> addDots(final LinearLayout linearLayout, Drawable backgount, int number){
        List<View> dots = new ArrayList<View>();
        for (int i = 0; i < number; i++) {
            int dotId = addDot(linearLayout,backgount);
            dots.add(findViewById(dotId));
        }
        return dots;
    }

}
